<template>
    <div class="pc-container">
        <Card style="width:320px;">
            <Form ref="loginForm" :model="loginForm" :rules="ruleForm" :label-width="80">
                <FormItem label="手机号码" prop="phonenumber">
                    <Input type="text" v-model="loginForm.phonenumber" placeholder="请输入手机号码"/>
                </FormItem>
                <FormItem label="验证码" prop="code">
                   <Input v-model="loginForm.code" placeholder="请输入验证码">
                       <Button :disabled="canClick" slot="append"  @click="handlerCode">{{codeText}}</Button>
                    </Input>
                </FormItem>
                <FormItem label="名称">
                    <Input v-model="loginForm.name" placeholder="请输入公司或个人名称（选填）"/>
                </FormItem>
                <FormItem>
                    <Button :loading="loading" type="primary" style="width:100%;" @click="handleRegister('loginForm')">
                          <span v-if="!loading">注&nbsp;&nbsp;&nbsp;册</span>
                          <span v-else>Loading...</span>
                    </Button>
                </FormItem>
            </Form>
         </Card>
    </div>
</template>
<script>
import {validPhoneNumber} from '@/utils/validate'
    export default {
        data () {
            const validatePhoneNumber=(rule,value,callback)=>{
                if(value===''){
                    callback(new Error('手机号码不能为空'))
                }
                else if(!validPhoneNumber(value)){
                    callback(new Error('请输入正确的手机号码'))
                }
                else{
                    callback()
                }
            }
            return {
                loading:false,
                loginForm: {
                    phonenumber: '',
                    code: ''
                },
                canClick:false,
                codeText:'获取验证码',
                totalTime:60,
                ruleForm: {
                    phonenumber: [{ required: true, trigger: 'blur',validator:validatePhoneNumber }],
                    code: [
                        { required: true, message: '验证码不能为空', trigger: 'blur' },
                        { type: 'string', min: 6,max:6, message: '验证码长度为6位字符', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            //注册事件
            handleRegister(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.loading=true
                        this.$store.dispatch('register',this.loginForm).then(()=>{
                            this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
                            this.loading=false
                        }).catch(()=>{
                            this.loading=false
                        })
                    } else {
                        this.$Message.error('请输入正确的手机号码和验证码')
                    }
                })
            },
            //获取验证码事件
            handlerCode(){
                 if(this.totalTime===60){
                    this.canClick=true
                    let clock = window.setInterval(() => {
                    this.totalTime--
                    this.codeText = this.totalTime + 's后重新发送'
                    if (this.totalTime < 0) {     //当倒计时小于0时清除定时器
                        window.clearInterval(clock)
                        this.codeText = '重新发送验证码'
                        this.totalTime = 60
                        this.canClick=false
                        }
                    },1000)
                    }
                    else{
                        this.$Message.error(this.totalTime+'s后重新发送')
                    }
            }
        }
    }
</script>
